<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>首页</title>
  <style>
    * {
      padding: 0;
      margin: 0;
      list-style: none;
    }

    .search {
      margin: 0 auto;
      width: 1400px;
      height: 40px;
      border: 1px solid black;
      align-content: center;
    }

    .content {
      padding-top: 10px;
      margin: 0 auto;
      width: 1400px;
      /* border: 1px solid black; */
      align-content: center;
    }

    table {
      width: 100%;
      text-align: center;
    }

    table,
    tr,
    th,
    td {
      border: 1px solid orange;
      border-collapse: collapse;
    }

    tr {
      height: 40px;
    }
  </style>
</head>

<body>
  <div class="search">
    姓名：<input type="text" class="name" id="name"><button class="btn">搜索</button><a href="add.html">添加</a>
  </div>
  <div class="content">
    <table>
      <thead>
        <tr>
          <th>序号</th>
          <th>学号</th>
          <th>姓名</th>
          <th>班级</th>
          <th>性别</th>
          <th>年龄</th>
          <th>电话</th>
          <th>爱好</th>
          <th>地址</th>
          <th>备注</th>
          <th>日期</th>
          <th>操作</th>
        </tr>
      </thead>
      <tbody>

      </tbody>
    </table>
  </div>
  <script>
    function getData(uname) {
      let xhr = new XMLHttpRequest()
      xhr.onreadystatechange = function () {
        if (xhr.readyState == 4) {
          if (xhr.status >= 200 && xhr.status <= 304) {
            let arr = JSON.parse(xhr.response)
            let str = ``
            arr.forEach((element, index) => {
              str += `
              <tr>
                <td>${index + 1}</td>
                <td>${element.id}</td>
                <td>${element.name}</td>
                <td>${element.clazz}</td>
                <td>${element.gender}</td>
                <td>${element.age}</td>
                <td>${element.tel}</td>
                <td>${element.hobby}</td>
                <td>${element.address}</td>
                <td>${element.remark}</td>
                <td>${element.date}</td>
                <td>
                  <a href='edit.html#${element.id}'>编辑</a><a href='javascript:del(${element.id})'>删除</a> 
                </td>
              
              </tr>
              `
            })
            document.querySelector('tbody').innerHTML = str
          }
        }
      }
      let url = 'http://localhost:3008/api/student/getStudent'
      if (uname) {
        url += '?name=' + uname
      }
      xhr.open('GET', url)
      xhr.send()
    }
    getData()
    var btn = document.querySelector('.btn')
    btn.addEventListener('click', function () {
      var uname = document.querySelector('#name').value
      getData(uname)
    })
    function del(id) {
      let xhr = new XMLHttpRequest()
      xhr.onreadystatechange = function () {
        if (xhr.readyState == 4) {
          if (xhr.status >= 200 && xhr.status <= 304) {
            let arr = JSON.parse(xhr.response)
            let str = ``
            arr.forEach((element, index) => {
              str += `
              <tr>
                <td>${index + 1}</td>
                <td>${element.id}</td>
                <td>${element.name}</td>
                <td>${element.clazz}</td>
                <td>${element.gender}</td>
                <td>${element.age}</td>
                <td>${element.tel}</td>
                <td>${element.hobby}</td>
                <td>${element.address}</td>
                <td>${element.remark}</td>
                <td>${element.date}</td>
                <td>
                  <a href=''>编辑</a><a href='javascript:del(${element.id})'>删除</a> 
                </td>
              
              </tr>
              `
            })
            document.querySelector('tbody').innerHTML = str
          }
        }
      }
      let url = 'http://localhost:3008/api/student/removeStudent'
      xhr.open('POST', url)
      xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
      xhr.send('id=' + id)
    }
  </script>
</body>

</html>